﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using EyFramework.SocketServer.InData;
using EyFramework.SocketServer.OutData;
using Newtonsoft.Json;
using WebSocketSharp;
using WebSocketSharp.Server;

namespace EyFramework.SocketServer.Behavior
{
    /**
     * 对当前的WebSockert服务进行管理
     */

    public class SysManagerBehavior : WebSocketBehavior
    {
        public enum SysManagerCommand
        {
            Stop = 1,
            Restart = 2
        }

        protected override void OnMessage(MessageEventArgs e)
        {
            string inData = e.Data;
            try
            {
                SysManagerInData sysManagerInData = JsonConvert.DeserializeObject<SysManagerInData>(inData);
                switch ((SysManagerCommand)sysManagerInData.Command)
                {
                    case SysManagerCommand.Stop:
                        Application.Exit(); //关闭服务
                        break;

                    case SysManagerCommand.Restart:
                        Application.Restart(); //重启服务
                        break;
                }
            }
            catch (Exception ex)
            {
                Send(ReturnResult.Fail(ex.Message, ex));
            }
        }
    }
}